package starter;

import gare.Gare;

import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.HashMap;

import thread.Train;
import thread.Voyageur;
import billetterie.Billeterie;
import billetterie.Trajet;

public class Starter {
	
	public static HashMap<String, Gare>	gares	= new HashMap<String, Gare>();
	
	public static Trajet RANDOM_TRAJET(ArrayList<Trajet> list) {
		int index = (int) (Math.random() * list.size());
		System.out.println(index);
		return list.get(index);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Billeterie b = null;
		System.out.print("Installing security manager...");
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new SecurityManager());
		}
		System.out.println("done.");
		
		System.out.print("Getting the registry...");
		Registry registry = null;
		try {
			registry = LocateRegistry.getRegistry();
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println("done.");
		
		System.out.print("Getting the stub...");
		try {
			b = (Billeterie) registry.lookup("sncf");
		} catch (AccessException e1) {
			e1.printStackTrace();
		} catch (RemoteException e1) {
			e1.printStackTrace();
		} catch (NotBoundException e1) {
			e1.printStackTrace();
		}
		System.out.println("done.");
		
		int NB_TRAINS = 1;
		int NB_VOYAGEUR = 5;
		
		Gare gare_A = new Gare();
		Gare gare_B = new Gare();
		Gare gare_C = new Gare();
		
		gares.put("A", gare_A);
		gares.put("B", gare_B);
		gares.put("C", gare_C);
		
		ArrayList<Trajet> trajets = new ArrayList<Trajet>();
		trajets.add(new Trajet("A", "B"));
		trajets.add(new Trajet("B", "A"));
		trajets.add(new Trajet("A", "C"));
		trajets.add(new Trajet("C", "A"));
		trajets.add(new Trajet("B", "C"));
		trajets.add(new Trajet("C", "B"));
		System.out.println(trajets.size() + " trajets sont créés.");
		
		for (int i = 0; i < NB_TRAINS; i++)
			new Train(i, RANDOM_TRAJET(trajets)).start();
		
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		for (int i = 0; i < NB_VOYAGEUR; i++)
			new Voyageur(i, RANDOM_TRAJET(trajets)).start();
		
	}
}
